<template>
  <ul class="alphablt">
    <li class="item" v-for="item of letters" :key="item" :ref="item" @click="handleLetterClick" @touchstart.prevent="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd">{{item}}</li>
  </ul>
</template>
<script>
export default {
  data() {
    return {
      touchStatus: false,
      // startY: 0,
      timer: null
    };
  },
  props: {
    cities: Object
  },
  // updated() {
  //   this.startY = this.$refs["A"][0].offsetTop
  // },
  computed: {
    letters() {
      let letters = [];
      for (let letter in this.cities) {
        letters.push(letter);
      }
      return letters;
    },
    startY () {
      return this.$refs["A"][0].offsetTop
    }
  },
  methods: {
    handleLetterClick(e) {
      this.$emit("change", e.target.innerText);
    },
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      if (this.touchStatus) {
        if (!this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          let touchY = e.touches[0].clientY - 79;
          let index = Math.floor((touchY - this.startY) / 22);
          this.$emit("change", this.letters[index]);
        }, 20);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    }
  }
};
</script>
<style lang="scss" scoped>
@import "~styles/varibles.scss";
.alphablt {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  position: absolute;
  right: 0;
  // top: 1.58rem;
  top: 1.58rem;
  bottom: 0;
  width: 0.4rem;
  .item {
    line-height: 0.45rem;
    color: $baseColor;
    text-align: center;
  }
}
</style>


